home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7274 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.8 KB  |  126 lines

  1. Path: news.dseg.ti.com!news    
  2. From: grubin@ti.com (Geoffrey Rubin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: [Q] OOD/inheritance Problem
  5. Date: 22 Feb 1996 13:56:14 GMT
  6. Organization: AWP
  7. Message-ID: <4ghslu$dld@mksrv1.dseg.ti.com>
  8. References: <4g53i1$jrr@ixnews3.ix.netcom.com>
  9. NNTP-Posting-Host: cna0185662.dseg.ti.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4g53i1$jrr@ixnews3.ix.netcom.com>, fedinv@ix.netcom.co 
  15. says...
  16. >
  17. >I have two classes which form a handle/body pair. So, one class ( 
  18. class
  19. >A, the handle) is seen by clients, and class A HAS-A class B ( the 
  20. body
  21. >) which is hidden from clients. Hence we have the following situation 
  22. -
  23. >
  24. >// ...Handle
  25. >class A 
  26. >{
  27. >        A( int n ) { if  ( n == x ) b = new ( C ) ;
  28. >                    elseif ( n == y ) b = new ( D ) ;
  29. >                    else b = new (E) ; } ;              
  30. >        ~A() { } ;
  31. >        .
  32. >        .
  33. >        A_f1 ( )  { b->B_f1( ) ; } ;  // forward request to B
  34. >        A_f2 ( )  { b->B_f2( ) ; } ;  //    ô   ô
  35. >        .
  36. >        .
  37. >     private:
  38. >        B   *b ;   // A HAS-A B
  39. >} ;
  40. >
  41. >//... Body ( is an ABC  )
  42. >class B
  43. >{
  44. >        .
  45. >        .
  46. >        virtual B_f1 ( ) = 0 ;
  47. >        virtual B_f2 ( ) ;
  48. >        .
  49. >        .
  50. >} ;
  51. >
  52. >Class AÆs purpose is to shield clients from the details of class B 
  53. and
  54. >all it does is merely forward requests to B. Class B is an abstract
  55. >base class for class C, D and E. Classes C,D and E implement the
  56. >functions B_f1, B
  57. >_f2 etc.. However, class E has its own member functions. Hence class 
  58. E
  59. >would look something like this - 
  60. >
  61. >class E : public B
  62. >{
  63. >        .
  64. >        .
  65. >        B_f1 ( )  { } ; 
  66. >        B_f2 ( ) { } ;
  67. >        E_f1 ( ) { } ;
  68. >        .
  69. >        .
  70. >} ;
  71. >
  72. >I would like to invoke E-f1() using the b pointer in class A. Hence
  73. >inside A, I would like to do the following - 
  74. >class A
  75. >{
  76. >        .
  77. >        .
  78. >        A_f3 ( ) { b->E_f1 () ; } ;
  79. >} ;
  80. >
  81. >This WILL NOT work ... snip
  82.  
  83. There is two direct ways to do this. The first is the object oriented 
  84. way.
  85.  
  86. You must add in a E_f1 method in the base class. The base class can 
  87. define this operation as a null behavior. This allows you to perform 
  88. this operation on any object inherited from B.
  89.  
  90. The second way (assuming that the first way is not applicable), is to
  91. define a method in B that returns a type (code). This allows you to 
  92. query the object and if it is of type E, to perform the type cast and 
  93. use the method E_f1. This type of code should be avoided since it is 
  94. harder to maintain.
  95.  
  96. I have used this second method in code. I didn't like it at all. 
  97. Therefore I redesigned the objects so that the first method could be 
  98. used. I will give a brief example.
  99.  
  100. Second Method: (ugly)
  101. A Shape base class performs creation, shape kind determination 
  102. and drawing of itself. Many different shapes are derived (inheritied) 
  103. from this Shape class. Interactive Editing routines perform 
  104. translation, scaling and rotation operations that are shape specific. 
  105. Therefore the editing functions used a switch statement based on shape 
  106. type to perform these operations (ugly). This is bad since adding
  107. a new shape, requires changing the editing operations.
  108.  
  109. First (prefered method):
  110. Redesign the shape base class to include general methods for 
  111. translation, scaling and rotation. The editing routines then simply 
  112. determine what to do. For example; traslate over 5.5 and down 10.2 
  113. units or rotated by 13.3 radians.
  114.  
  115. The specific shapes derived from the Shape class must override the 
  116. default behavior whenever this is not the desired result.
  117.  
  118. For example the Shape translation method may work for all shapes 
  119. (assumming the position is within the Shape base class. The Shape 
  120. rotation method may provide no action since many objects are rotation 
  121. independent (a point, a circle etc.).
  122.  
  123. Geoffrey
  124.  
  125.  
  126.